<template>
  <div class="app-page">
    <el-table-form ref="form" :model="query">
      <el-form-item class="merge-item">
        <el-select v-model="csmType" style="width: 170px; margin-right: 2px;">
          <el-option label="钢瓶编号" value="bottleCode" />
          <el-option label="镂空码" value="unifiedCode" />
        </el-select>
        <el-input v-model="csmText" placeholder="请输入" clearable />
      </el-form-item>
      <el-form-item label="状态:">
        <el-select v-model="query.bottleStatus" filterable clearable placeholder="请选择">
          <el-option
            v-for="item in bottleStatusMap"
            :key="item.value"
            :label="item.desc"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <template v-slot:btns>
        <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
      </template>
    </el-table-form>
    <lb-table
      ref="lbTable"
      v-loading="$loadingPlugin.fetchData"
      v-el-height-adaptive-table="{bottomOffset:10}"
      border
      highlight-current-row
      element-loading-text="加载中"
      header-row-class-name="table-row-name"
      :column="column"
      :data="tableData.data"
      pagination
      :total="tableData.total"
      custom-column
      @fetch="fetchData"
    />
  </div>
</template>

<script>
  import { queryCylinderSuperviseList, queryCylinderStatusMap, changeCylinderStatus } from '@/api'
  import { formatColumnMap, parseTime } from '@/utils'

  export default {
    name: 'cylinderSupervise',
    data() {
      return {
        actionColWidth: 80,
        bottleStatusMap: [],
        csmType: 'bottleCode',
        csmText: '',
        query: {
          bottleStatus: ''
        },
        tableData: {
          total: 0,
          data: []
        }
      }
    },
    computed: {
      column: function() {
        return [
          {
            prop: 'bottleCode',
            label: '钢瓶编码'
          },
          {
            prop: 'chipCode',
            label: '芯片编码'
          },
          {
            prop: 'unifiedCode',
            label: '镂空码'
          },
          {
            prop: 'lastCheckDt',
            label: '最后充装时间',
            width: 160,
            render: (h, { row }) => <span>{ row.lastCheckDt && parseTime(row.lastCheckDt, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
          },
          {
            prop: 'lastSendDt',
            label: '最后配送时间',
            width: 160,
            render: (h, { row }) => <span>{ row.lastSendDt && parseTime(row.lastSendDt, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
          },
          {
            prop: 'orgName',
            width: 160,
            label: '最后配送机构'
          },
          {
            prop: 'attacherName',
            label: '最后配送人',
            width: 100
          },
          {
            prop: 'bottleStatus',
            width: 60,
            label: '状态',
            render: (h, { row }) => <span>{ row.bottleStatus && formatColumnMap(this.bottleStatusMap, 'value', row.bottleStatus, 'desc') }</span>
          },
          {
            prop: 'lastUpdateTime',
            label: '最后变更时间',
            width: 160,
            render: (h, { row }) => <span>{ row.lastUpdateTime && parseTime(row.lastUpdateTime, '{y}-{m}-{d} {h}:{i}:{s}') }</span>
          },
          {
            prop: 'lastUpdateUserName',
            width: 100,
            label: '最后变更人'
          },
          {
            label: '操作',
            className: 'action-col',
            width: this.actionColWidth,
            fixed: 'right',
            render: (h, { row, $index }) => {
              return (
                <div>
                  {(row.bottleStatus === 1 || row.bottleStatus === 2) && <el-link size='mini' underline={false} onClick={() => this.del(row, $index)}>
                    状态变更
                  </el-link>}
                </div>
              )
            }
          }
        ]
      }
    },
    created() {
      this.queryOptionsMap()
    },
    methods: {
      _getQuery(obj) {
        const query = {}
        for (const key in obj) {
          if (obj.hasOwnProperty(key)) {
            if (obj[key] !== '') {
              query[key] = obj[key]
            }
          }
        }
        return query
      },
      async fetchData({ currentPage, pageSize }) {
        const query = {
          ...this._getQuery(this.query),
          currentPage,
          pageSize
        }
        query[this.csmType] = this.csmText
        const res = await queryCylinderSuperviseList(query)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
        this.flexColumnWidth()
      },
      // 查询枚举
      async queryOptionsMap() {
        const res = await queryCylinderStatusMap()
        this.bottleStatusMap = JSON.parse(res.data.bottleStatus) // 状态
      },
      del(row, index) {
        const str1 = row.bottleStatus === 1 ? '重瓶' : '轻瓶'
        const str2 = row.bottleStatus === 1 ? '轻瓶' : '重瓶'
        this.$confirm(`是否将当前状态：${str1}——>  变更为:${str2}`, '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$message({
            message: '正在变更',
            duration: 0,
            iconClass: 'el-icon-loading'
          })
          changeCylinderStatus({ chipCode: row.chipCode }).then(() => {
            this.$refs.lbTable.fetch()
            this.$notify({
              title: '提示',
              message: '变更钢瓶监管状态成功',
              type: 'success',
              duration: 2000
            })
          }).finally(() => {
            loading.close()
          })
        })
      },
      search() {
        this.$refs.lbTable.fetch({ currentPage: 1 })
      },
      // 自适应表格列宽
      flexColumnWidth() {
        this.$nextTick(() => {
          let max = 0
          const actionColDom = document.getElementsByClassName('action-col')
          for (let i = 0; i < actionColDom.length; i++) {
            if (actionColDom[i].tagName === 'TD') {
              let width = 20
              const linkDom = actionColDom[i].childNodes[0].childNodes[0].childNodes
              linkDom.forEach(it => {
                width += (it.offsetWidth + 10)
              })
              if (width > max) max = width
            }
          }
          this.actionColWidth = max > 60 ? max - 10 : 50
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.el-form-item .el-form-item {
  margin-bottom: 0;
}
.merge-item {
  /deep/ .el-form-item__content {
    display: flex;
  }
}
.btn-group {
  display: flex;
  justify-content: space-between;
}
</style>
